package com.glory.dmegcpda.viewmodel

import com.glory.dmegcpda.model.ScribingRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject

class ScribingViewModel : BaseViewModel() {

    val scribingRepository by lazy{ScribingRepository()}

    fun initCaseData(id: String) {
        val bodyMap = JsonObject()
        bodyMap.addProperty("ENTITYMODEL", "com.glory.mes.mm.lot.model.MLot")
        bodyMap.addProperty("WHERECLAUSE", "mLotId='"+id+"'")
        launchOnlyResult({
            val scribingcase = scribingRepository.scribingCase(bodyMap)
            if (scribingcase.dATALIST.isNotEmpty()){
                defUI.callObserve.value = CallObserveBean(1,scribingcase)
            }else{
                defUI.toastEvent.value = ToastMessageBean("无返回数据，请检查输入信息是否正确？",true)
            }
        })
    }

    fun initEqpData(id: String) {
        val bodyMap = JsonObject()
        bodyMap.addProperty("ENTITYMODEL", "com.glory.mes.ras.eqp.PositionSetLine")
        bodyMap.addProperty("WHERECLAUSE", "positionName='"+id+"'")
        launchOnlyResult({
            val scribingeqp = scribingRepository.scribingEqp(bodyMap)
            if (scribingeqp.dATALIST.isNotEmpty()){
                defUI.callObserve.value = CallObserveBean(2,scribingeqp)
            }else{
                defUI.toastEvent.value = ToastMessageBean("无返回数据，请检查输入信息是否正确？",true)
            }
        })
    }

    fun initBoxData(id: String) {
        val bodyMap = JsonObject()
        bodyMap.addProperty("ENTITYMODEL", "com.glory.mes.mm.durable.model.Carrier")
        bodyMap.addProperty("WHERECLAUSE", "durableId='"+id+"'")
        launchOnlyResult({
            val scribingbox = scribingRepository.scribingBox(bodyMap)
            if (scribingbox.dATALIST.isNotEmpty()){
                defUI.callObserve.value = CallObserveBean(3,scribingbox)
            }else{
                defUI.toastEvent.value = ToastMessageBean("无返回数据，请检查输入信息是否正确？",true)
            }
        })
    }

    fun initData(caseCode: String, eqpCode: String, boxCode: String) {
        val bodyMap = JsonObject()
        bodyMap.addProperty("MLOTID", caseCode)
        bodyMap.addProperty("POSITIONNAME", eqpCode+"#POSITIONNAME")
        bodyMap.addProperty("DURABLEID", boxCode)
        launchOnlyResult({
            scribingRepository.scribing(bodyMap)
            defUI.callObserve.value = CallObserveBean(4)
        })
    }
}